package com.xkdx.caipiao.redpacke;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.unionpay.UPPayAssistEx;
import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.WebActivity;
import com.xkdx.caipiao.application.MyApplication;
import com.xkdx.caipiao.fragment.UserInfoFragment;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.hongbaolist.HongbaoListInfo;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.presistence.recharge.RechargeAction;
import com.xkdx.caipiao.presistence.recharge.RechargeModule;
import com.xkdx.caipiao.presistence.recharge.RechargePresistence;
import com.xkdx.caipiao.presistence.redrecharge.RedRechargeAction;
import com.xkdx.caipiao.presistence.redrecharge.RedRechargeModule;
import com.xkdx.caipiao.presistence.redrecharge.RedRechargePresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;
import com.xkdx.caipiao.util.SharePrefenceUtil;

public class RedpacketExchange extends ThreadActivity implements
		OnClickListener {
	RedRechargeAction action;
	RedRechargeModule module;
	RedRechargePresistence presistence;
	static String UPMPAY = "upmppay";
	static String ALIPAY = "alipay";
	String payway = "alipay";
	/*private HongbaopayAction action;
	private HongbaopayModule module;
	private HongbaopayPresistence presistence;*/
	private String index = "1", size = "10", uid, token;
	private SharePrefenceUtil su;
	private TextView tv_money1;
	private TextView tv_money2;
	private ImageView upmppay;
	private ImageView alipay;
	private Button button1;
	private String packetId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.redpacket_exc_caipiao);
		init();
		setClick();
	}

	private void init() {
		tv_money1 = (TextView) findViewById(R.id.tv_money1);
		tv_money2 = (TextView) findViewById(R.id.tv_money2);
		Intent intent = getIntent();
		String name = intent.getStringExtra("name");
		String money = intent.getStringExtra("money");
		tv_money1.setText(name);
		tv_money2.setText(money);
		upmppay = (ImageView) findViewById(R.id.upmppay);
		alipay = (ImageView) findViewById(R.id.alipay);
		alipay.setImageResource(R.drawable.bg_bank_selected_caipiao);
		
		button1 = (Button) findViewById(R.id.bt_pay);

	}

	private void setClick() {
		upmppay.setOnClickListener(this);
		alipay.setOnClickListener(this);
		button1.setOnClickListener(this);
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			UserInfoFragment.isUpdate = true;
			IConstants.localUserMoney = 0;
			if (payway.equals(ALIPAY)) {
				if (module.url != null) {
					Intent intent = new Intent(this, WebActivity.class);
					intent.putExtra("url", module.url);
					intent.putExtra("title", "支付宝");
					startActivity(intent);

				}
			} else if (payway.equals(UPMPAY)) {
				if (module.tn != null) {
					String serverMode = "00";
					int ret = UPPayAssistEx.startPay(this, null, null,
							module.tn, serverMode);
					if (ret == UPPayAssistEx.PLUGIN_NOT_FOUND) {
						UPPayAssistEx.installUPPayPlugin(this);
					}
				}
			}

		}

	}

	@Override
	public void onBackClick(View view) {

		finish();
	}

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		Intent intent = getIntent();
		String money = intent.getStringExtra("money");
		packetId = intent.getStringExtra("id");
		money = money.substring(0, money.length()-1);
		Float f= Float.parseFloat(money);
		Math.abs(f);
		
		BigDecimal   b   =   new   BigDecimal(f);  
		double   f1   =   b.setScale(4,   BigDecimal.ROUND_HALF_UP).doubleValue();
		
		f1 *= 100;
		int et_money;
		if (!money.equals("")) {
			et_money = (int)f1;//Integer.valueOf(money) ;
		} else {
			et_money = 0;
		}

		if (et_money > 0) {
			LoginInfo info = getLoginInfo();
			String md5 = info.getId()+info.getLogintoken()+IConstants.key;
			String signcode = Md5Utils.md5(md5);
			action = new RedRechargeAction(info.getId(), info.getLogintoken(),
					payway, packetId,signcode);
			module = new RedRechargeModule();
			presistence = new RedRechargePresistence(this);
			presistence.setActitons(action);
			presistence.setModule(module);
			presistence.execute();
		} else {
			if (et_money == 0) {
				LoginInfo info = getLoginInfo();
				String md5 = info.getId()+info.getLogintoken()+IConstants.key;
				String signcode = Md5Utils.md5(md5);
				action = new RedRechargeAction(info.getId(), info.getLogintoken(),
						payway,packetId,signcode);
				module = new RedRechargeModule();
				presistence = new RedRechargePresistence(this);
				presistence.setActitons(action);
				presistence.setModule(module);
				presistence.execute();
			} 

		}

		

	}

	@Override
	public void onResume(){
		super.onResume();
		if(MyApplication.isClosePayDetail){
			MyApplication.isClosePayDetail = false;
			finish();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.upmppay:
			payway = UPMPAY;
			upmppay.setImageResource(R.drawable.bg_bank_selected_caipiao);
			alipay.setImageResource(0);
			break;
		case R.id.alipay:
			payway = ALIPAY;
			upmppay.setImageResource(0);
			alipay.setImageResource(R.drawable.bg_bank_selected_caipiao);
			break;
		case R.id.bt_pay:
				if (payway.equals("")) {
					Toast.makeText(this, "请选择支付方式", Toast.LENGTH_SHORT).show();
				} else {
					startThread();
				}
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {
		// TODO Auto-generated method stub
		if (data == null) {
			return;
		}
		String str = data.getExtras().getString("pay_result");
		Log.i("hz", "pay_result" + str);
		if (str.equalsIgnoreCase("")) {

		}
		MyApplication.isClosePacketList = true;
		MyApplication.isClosePayDetail = true;
		finish();

	}
}
